package com.net.xpay.admin.controller.admin;

import com.google.common.base.Preconditions;
import com.google.common.base.Strings;
import com.net.common.exception.BusinessException;
import com.net.common.model.ResultData;
import com.net.common.model.ResultPagedList;
import com.net.xpay.admin.vo.CommonSysConfigVo;
import com.net.xpay.common.domain.CommonSysConfig;
import com.net.xpay.common.enums.config.ValueType;
import com.net.xpay.common.manager.CommonSysConfigManager;
import com.net.xpay.common.model.CommonSysConfigModel;
import com.net.xpay.common.model.search.CommonSysConfigSearchModel;
import com.net.xpay.core.constant.PoseidonErrorCode;
import com.net.xpay.core.utils.BeanCopierUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.math.NumberUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/admin/common-sys-config")
@Api(tags = "运营/全局系统的配置类", description = "运营 全局系统的配置类")
public class AdminCommonSysConfigController {

    @Autowired
    private CommonSysConfigManager commonSysConfigManager;

    @ApiOperation("搜索")
    @PostMapping("/search")
    @ResponseBody
    @PreAuthorize("hasAnyRole('ADMIN', 'common-sys-config::search')")
    public ResultPagedList<CommonSysConfigVo> searchCommonSysConfig(@RequestBody CommonSysConfigSearchModel searchModel) {
        List<CommonSysConfig> records = commonSysConfigManager.search(searchModel);
        long count = commonSysConfigManager.searchCount(searchModel);

        List<CommonSysConfigVo> vos = BeanCopierUtil.batchCopy(CommonSysConfig.class, CommonSysConfigVo.class, records);
        return new ResultPagedList<>(vos, count, searchModel);
    }

    @ApiOperation("新增")
    @PostMapping("/save")
    @ResponseBody
    @PreAuthorize("hasAnyRole('ADMIN', 'common-sys-config::save')")
    public ResultData<Boolean> saveCommonSysConfig(@RequestBody CommonSysConfigModel model) {
        Preconditions.checkArgument(model != null );
        if (Strings.isNullOrEmpty(model.getKey())) {
            throw new BusinessException(PoseidonErrorCode.PARAMETER_ILLEGAL, "key不能为空");
        }
        if (Strings.isNullOrEmpty(model.getValue())) {
            throw new BusinessException(PoseidonErrorCode.PARAMETER_ILLEGAL, "值不能为空");
        }
        model.setKey(model.getKey().trim());
        model.setValue(model.getValue().trim());
        if (model.getValueType() == null) {
            throw new BusinessException(PoseidonErrorCode.PARAMETER_ILLEGAL, "类型不能为空");
        }
        if (model.getValueType() == ValueType.NUMBER) {
            if (!NumberUtils.isDigits(model.getValue())) {
                throw new BusinessException(PoseidonErrorCode.PARAMETER_ILLEGAL, "值类型不是数字类型");
            }
        }

        if (Strings.isNullOrEmpty(model.getNote())){
            throw new BusinessException(PoseidonErrorCode.PARAMETER_ILLEGAL, "备注不能为空");
        }
        CommonSysConfig commonSysConfig = new CommonSysConfig();
        commonSysConfig.setKey(model.getKey());
        commonSysConfig.setValue(model.getValue());
        commonSysConfig.setValueType(model.getValueType());
        commonSysConfig.setNote(model.getNote());

        commonSysConfigManager.save(commonSysConfig);

        return new ResultData<>(true);
    }

    @ApiOperation("修改")
    @PostMapping("/update")
    @ResponseBody
    @PreAuthorize("hasAnyRole('ADMIN', 'common-sys-config::update')")
    public ResultData<Boolean> updateCommonSysConfig(@RequestBody CommonSysConfigModel model) {
        Preconditions.checkArgument(model != null && model.getId() != null);
        if (Strings.isNullOrEmpty(model.getKey())) {
            throw new BusinessException(PoseidonErrorCode.PARAMETER_ILLEGAL, "key不能为空");
        }
        if (Strings.isNullOrEmpty(model.getValue())) {
            throw new BusinessException(PoseidonErrorCode.PARAMETER_ILLEGAL, "值不能为空");
        }
        model.setKey(model.getKey().trim());
        model.setValue(model.getValue().trim());
        if (model.getValueType() == null) {
            throw new BusinessException(PoseidonErrorCode.PARAMETER_ILLEGAL, "类型不能为空");
        }
        if (model.getValueType() == ValueType.NUMBER) {
            if (!NumberUtils.isDigits(model.getValue())) {
                throw new BusinessException(PoseidonErrorCode.PARAMETER_ILLEGAL, "值类型不是数字类型");
            }
        }

        if (Strings.isNullOrEmpty(model.getNote())){
            throw new BusinessException(PoseidonErrorCode.PARAMETER_ILLEGAL, "备注不能为空");
        }
        CommonSysConfig commonSysConfig = commonSysConfigManager.getById(model.getId());
        commonSysConfig.setKey(model.getKey());
        commonSysConfig.setValue(model.getValue());
        commonSysConfig.setValueType(model.getValueType());
        commonSysConfig.setNote(model.getNote());

        commonSysConfigManager.updateByPrimaryKeyWithVersion(commonSysConfig);

        return new ResultData<>(true);
    }

}
